package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.MealsDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Meals;
import com.pro_one.service.MealsService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(interfaceClass = MealsService.class)
@Transactional
public class MealsServiceImpl implements MealsService {
    @Autowired
    private MealsDao mealsDao;
    @Override
    public void upload(List<Meals> list) {
       if (list!=null&&list.size()>0){
           for (Meals meals : list) {
              this.addmeals(meals);
           }
       }
    }
/*分页查询*/
    @Override
    public PageResult findByCondition(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<Meals> page = mealsDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }
/*增加*/
    @Override
    public void add(Meals meals) {
       this.addmeals(meals);
    }

    /*增加方法*/
    public void addmeals(Meals meals){
        String name = meals.getName();
        Integer count = mealsDao.findname(name);
        if (count>0) {
            mealsDao.modification(meals);
        }
        mealsDao.upload(meals);
    }
}
